package com.moxiao.yfjh.module.activity.service.activityPackage;

import com.moxiao.yfjh.framework.common.util.md5.Md5Utils;
import com.moxiao.yfjh.module.activity.common.GenerateVerificateCode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.moxiao.yfjh.module.activity.controller.admin.activityPackage.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityPackage.ActivityPackageDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.activityPackage.ActivityPackageConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.activityPackage.ActivityPackageMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.PACKAGE_NOT_EXISTS;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲活动套餐信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ActivityPackageServiceImpl implements ActivityPackageService {

    @Resource
    private ActivityPackageMapper packageMapper;
    @Resource
    private GenerateVerificateCode generateVerificateCode;

    /**
     * 测试类
     */
    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            System.out.println(genVerificateCode());
//        }

    }

    @Override
    public void createPackage(List<ActivityPackageCreateReqVO> createReqVO) {
        // 插入
        List<ActivityPackageDO> activityPackageDOS = ActivityPackageConvert.INSTANCE.convert(createReqVO);
        packageMapper.insertBatch(activityPackageDOS);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePackage(List<ActivityPackageUpdateReqVO> updateReqVO) {
        updateReqVO.forEach(activityPackageUpdateReqVO -> {
            // 校验存在
//            validatePackageExists(activityPackageUpdateReqVO.getId());
            // 更新
            ActivityPackageDO updateObj = ActivityPackageConvert.INSTANCE.convert(activityPackageUpdateReqVO);
            packageMapper.updateById(updateObj);
        });

    }

    @Override
    public void deletePackage(Long id) {
        // 校验存在
        validatePackageExists(id);
        // 删除
        packageMapper.deleteById(id);
    }

    private void validatePackageExists(Long id) {
        if (packageMapper.selectById(id) == null) {
            throw exception(PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public ActivityPackageDO getPackage(Long id) {
        return packageMapper.selectById(id);
    }

    @Override
    public List<ActivityPackageDO> getPackageList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return packageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ActivityPackageDO> getPackagePage(ActivityPackagePageReqVO pageReqVO) {
        return packageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityPackageDO> getPackageList(ActivityPackageExportReqVO exportReqVO) {
        return packageMapper.selectList(exportReqVO);
    }

    @Override
    public ActivityPackageDO getSelectOne(Long activeId, Long productId) {
        LambdaQueryWrapper<ActivityPackageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, ActivityPackageDO::getActiveId, activeId);
        wrapper.eq(true, ActivityPackageDO::getProductId, productId);
        wrapper.last("LIMIT " + 1);
        return packageMapper.selectOne(wrapper);
    }


    /**
     * 生成核销码-随机数
     */
    public Integer genVerificateCode() {
        Random random = new Random();
        return random.nextInt(99999999);
    }

    @Override
    public ActivityPackageDO getOne(ActivityPackagePageReqVO pageReqVO) {
        LambdaQueryWrapper<ActivityPackageDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, ActivityPackageDO::getActiveId, pageReqVO.getActiveId());
        wrapper.eq(true, ActivityPackageDO::getProductId, pageReqVO.getProductId());
        wrapper.last("LIMIT " + 1);
        return packageMapper.selectOne(wrapper);
    }
}
